<?php

class UserView {

	private $newPassword = "NewPassword";
	private $saveNewPassSubmit = "NewPassSubmit";
	
	private $newPasswordMessage;
	
	/**
	 * Function to generate a unordered list with Users in DB
	 * @param array with user objects
	 * @return html
	 */
	public function generateUserList($usersArray) {
		$returnDiv = "<div class='usersDiv'>
			<h2>All users | make your choise...</h2>
			<ul>";

		foreach ($usersArray as $key => $user) {
			if ($user -> IsAdmin) {
				continue;
			}
			$returnDiv .= "<li><a href='?" . NavigationView::controller . "=" . NavigationView::showUserController . "&" . NavigationView::userId . "=" . $user -> userId . "'>" . $user -> UserName . "</a></li>";
		}

		$returnDiv .= "</ul><div class='clear'></div>
		</div>";

		return $returnDiv;
	}

	/**
	 * function to generate a html div with user info
	 * @param User object
	 * @return html
	 */
	public function showUser(User $loggedInUser, User $displayUser) {
		//If we are looking at out own page, display change pass, else not	
		if($displayUser->UserName === $loggedInUser->UserName){
			return "<div class='userDiv'>
			<h2 class='userName'>$displayUser->UserName</h2>
			<p class='userEmail'>$displayUser->Email</p>
			<p>Want to change your password? Do it here:</p>
			$this->newPasswordMessage
			<form method='post' action=''>
				<fieldset>
					<legend><strong>Change password</strong></legend>
					<input type='password' name='$this->newPassword'/>
					<input type='submit' name='$this->saveNewPassSubmit' value='Save'/>
				</fieldset>
			</form>
		</div>";
		}	
		return "<div class='userDiv'>
			<h1 class='userName'>$displayUser->UserName</h1>
			<p class='userEmail'>$displayUser->Email</p></br>
		</div>";
	}
	
	/**
	 * Function to generate New password feedback
	 * @access public
	 * @param UserObject
	 * @return HTML
	 */
	public function setNewPasswordMessage($user = "no user"){
		if(is_object($user)){
			$this->newPasswordMessage = "<p class='noerrorP'>User $user->UserName has been updated, new password is:<strong> $user->PassWord </strong></p>";
		}
		else{
			$this->newPasswordMessage = "<p class='fileErrorP'>Password was <strong>NOT changed</strong></p>";
		}
	}
	
	/**
	 * Function to determin if user tried to update password 
	 * will make sure we dont hasve an empty string in new pass
	 * @access public
	 * @param -
	 * @return bool
	 */
	public function didUserChangePassword(){
		if(isset($_POST[$this->saveNewPassSubmit]) AND isset($_POST[$this->newPassword])){
			return true;
		}
		return false;
	}
	
	/**
	 * Function to get new password submitted by user
	 * will validate pass befor returning
	 * @access public
	 * @param -
	 * @return string - new password
	 */
	public function getNewPassword(){
		if(isset($_POST[$this->newPassword]) AND InputValidationHandler::isValidPass($_POST[$this->newPassword])){
			return $_POST[$this->newPassword];
		}
		return false;
	}

}
